package com.sz.lk.wingsystem.mvp.model;

import com.google.gson.Gson;
import com.sz.lk.wingsystem.bean.TaskResult;
import com.sz.lk.wingsystem.bean.TaskReturnPamars;
import com.sz.lk.wingsystem.bean.TaskReturnResult;
import com.sz.lk.wingsystem.mvp.BaseRequest;
import com.sz.lk.wingsystem.mvp.OnNetRequestListener;
import com.sz.lk.wingsystem.mvp.RxPresenter;
import com.sz.lk.wingsystem.mvp.contract.TaskContract;
import com.sz.lk.wingsystem.mvp.rxuntils.RxScheduler;
import com.sz.lk.wingsystem.mvp.rxuntils.Subscribe2Help;

import io.reactivex.Observable;
import okhttp3.RequestBody;

/**
 * author : liuke
 * e-mail : 1012459343@qq.com
 * date   : 2020/12/6 18:09
 * desc   :
 * version: 1.0
 */
public class TaskModel extends BaseRequest implements TaskContract.Model {
    public TaskModel(RxPresenter mRxPresenter) {
        super(mRxPresenter);
    }

    @Override
    public void task(String code, OnNetRequestListener<TaskResult> listener) {
        Observable<TaskResult> observable = webRequest.LoadTaskInfoByCode(code);
        listener.onStart();
        observable.compose(RxScheduler.<TaskResult>Obs_io_main())

                .subscribe(new Subscribe2Help<TaskResult>(mRxPresenter) {
                    @Override
                    public void onNext(TaskResult value) {
                        listener.onSuccess(value);
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        super.onError(throwable);
                        listener.onFailure(throwable);
                    }

                    @Override
                    public void onComplete() {
                        listener.onFinish();
                    }
                });
    }

    @Override
    public void UploadTaskInfo(String id, String code, double weight, double volume,
                               double length, double width,
                               double height, String estimateChannel,
                               String estimateAddress, OnNetRequestListener<TaskReturnResult> listener) {
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                new Gson().toJson(new TaskReturnPamars(id, code, weight, volume,
                        length, width, height, estimateChannel, estimateAddress)));
        Observable<TaskReturnResult> observable = webRequest.UploadTaskInfo(requestBody);
        listener.onStart();
        observable.compose(RxScheduler.<TaskReturnResult>Obs_io_main())

                .subscribe(new Subscribe2Help<TaskReturnResult>(mRxPresenter) {
                    @Override
                    public void onNext(TaskReturnResult value) {
                        listener.onSuccess(value);
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        super.onError(throwable);
                        listener.onFailure(throwable);
                    }

                    @Override
                    public void onComplete() {
                        listener.onFinish();
                    }
                });
    }
}